﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;

namespace HOOMT
{
    class HighOrderObjectDiagramDBO : OriginalDiagramDBO
    {
        public bool AddHOOD(HighOrderObjectDiagram hood)
        {
            if (this.GetHOODById(hood.Id) != null)
            {
                this.UpdateHOOD(hood);
                return true;
            }
            else
            {

                try
                {
                    //   this.conn = this.GetConnection();
                    SQLiteCommand cmd = this.GetCmd();
                    cmd.CommandText = "insert into HighOrderObjectDiagram( id ,x ,y,width,height,name,isSelected,newModelFromId,treeNode) "
                       + " values (@id,@x,@y,@width,@height,@name,@isSelected,@newModelFromId,@treeNode)";
                    cmd.Parameters.Add(new SQLiteParameter("id", hood.Id));
                    cmd.Parameters.Add(new SQLiteParameter("x", hood.UpLeftpoint.X));
                    cmd.Parameters.Add(new SQLiteParameter("y", hood.UpLeftpoint.Y));
                    cmd.Parameters.Add(new SQLiteParameter("width", hood.Width));
                    cmd.Parameters.Add(new SQLiteParameter("height", hood.Height));
                    cmd.Parameters.Add(new SQLiteParameter("name", hood.Name));
                    cmd.Parameters.Add(new SQLiteParameter("isSelected", hood.IsSelected));
                    if (hood.HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFromId", hood.NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFromId", null));
                    }

                    cmd.Parameters.Add(new SQLiteParameter("treeNode", hood.treeNode));
                    cmd.ExecuteNonQuery();
                    this.CloseConnection();
                    return true;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return false;
                }
            }

        }

        public int AddHOODList(List<HighOrderObjectDiagram> HOODList)
        {
            foreach (HighOrderObjectDiagram hood in HOODList)
            {
                this.AddHOOD(hood);
            }
            return HOODList.Count;
        }

        public void ShowAllInConsole()
        {
            SQLiteCommand cmd = this.GetCmd();
            cmd.CommandText = "select * from HighOrderObjectDiagram";
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["id"] + "\t" + reader["name"] + "\t" + reader["width"]);
            }
        }

        public HighOrderObjectDiagram GetHOODById(String id)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from HighOrderObjectDiagram where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                SQLiteDataReader  reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    HighOrderObjectDiagram hood = new HighOrderObjectDiagram(
                        new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()))
                        , int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                    return hood;
                }
                else
                {
                    return null;
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }

        public bool UpdateHOOD(HighOrderObjectDiagram hood)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "update HighOrderObjectDiagram( id ,x ,y,width,height,name,isSelected,newModelFromId,treeNode) "
                  + " values (@id,@x,@y,@width,@height,@name,@isSelected,@newModelFromId,@treeNode) where id=@id1";
                cmd.Parameters.Add(new SQLiteParameter("id", hood.Id));
                cmd.Parameters.Add(new SQLiteParameter("x", hood.UpLeftpoint.X));
                cmd.Parameters.Add(new SQLiteParameter("y", hood.UpLeftpoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("width", hood.Width));
                cmd.Parameters.Add(new SQLiteParameter("height", hood.Height));
                cmd.Parameters.Add(new SQLiteParameter("name", hood.Name));
                cmd.Parameters.Add(new SQLiteParameter("isSelected", hood.IsSelected));
                if (hood.HasModelForm)
                {
                    cmd.Parameters.Add(new SQLiteParameter("newModelFromId", hood.NewModelForm.id));
                }
                else
                {
                    cmd.Parameters.Add(new SQLiteParameter("newModelFromId", null));
                }

                cmd.Parameters.Add(new SQLiteParameter("treeNode", hood.treeNode));
                cmd.Parameters.Add(new SQLiteParameter("id1", hood.Id));
                cmd.ExecuteNonQuery();
                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        public bool DeleteHOODById(String id)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from HighOrderObjectDiagram where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                cmd.ExecuteNonQuery();
                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }



    }
}
